/*
    This sketch sends data via HTTP GET requests to data.sparkfun.com service.

    You need to get streamId and privateKey at data.sparkfun.com and paste them
    below. Or just customize this script to talk to other HTTP servers.

*/

#include <WiFi.h>
#include <PubSubClient.h>
#include <Ticker.h>


#define mqtt_devid "626108102"  //�豸ID
const char* ssid     = "jaki";
const char* password = "riki0809";
#define JDQ 2

char msg_buf[200];                                //������Ϣ������
char dataTemplate[] = "{\"temp\":%.2f,\"hum\":%.2f}"; //��Ϣģ��
char msgJson[75];                                 //Ҫ���͵�json��ʽ������
unsigned short json_len = 0;                      //json����
Ticker tim1;                                      //��ʱ��,����ѭ���ϴ�����


const char* MQTT_SERVER  = "39.97.5.49";
const int   MQTT_PORT    = 1883;
const char* MQTT_USRNAME = "robot";
const char* MQTT_PASSWD  = "123456";
const char* SUB_TOPIC = "lightctrl";
const char* PUB_TOPIC = "motorinfo";
const char* CLIENT_ID    = "scy-mqtt-client";  //��ǰ�豸��clientid��־

WiFiClient espClient;
PubSubClient  client(espClient);
long lastMsg = 0;

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);   // ��ӡ������Ϣ
  Serial.print("] ");
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]); // ��ӡ��������
  }

  if ((char)payload[0] == '1') {
    digitalWrite(JDQ, HIGH);   // ����
    Serial.print(" Light On");
  } else {
    digitalWrite(JDQ, LOW);   // Ϩ��
    Serial.print(" Light Off");
  }

  Serial.println();
}

//�����ⷢ��ģ�����ʪ������
void sendTempAndHumi()
{
  if (client.connected())
  {
    snprintf(msgJson, 40, dataTemplate, 22.31, 25.92); //��ģ����ʪ����������dataTemplateģ����, ���ɵ��ַ�������msgJson
    Serial.print("public message:");
    Serial.println(msgJson);
    client.publish(PUB_TOPIC, (uint8_t *)msgJson, strlen(msgJson)); //�������ݵ�����$dp
  }
}

void setup()
{
  Serial.begin(115200);
  delay(10);

  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

  pinMode(JDQ, OUTPUT);
  client.setServer(MQTT_SERVER, MQTT_PORT); 
  client.setCallback(callback);   
  tim1.attach(10, sendTempAndHumi);                      //��ʱÿ20�����һ�η������ݺ���sendTempAndHumi
    
}

int value = 0;

void reconnect() {
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    if (client.connect(CLIENT_ID,MQTT_USRNAME,MQTT_PASSWD)) {
      Serial.println("connected");
      // ���ӳɹ�ʱ��������
      client.subscribe(SUB_TOPIC);
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      delay(5000);
    }
  }
}

void loop()
{
  if (!client.connected()) {
    reconnect();
  }
  client.loop();
}
